﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
[LuaCallCSharp]
public class MyOBJ :MonoBehaviour{
	public int i;
	public int d;
	public GameObject obj;
	public List<ObjData> l_ListObj = new List<ObjData>();	
	public Dictionary<string,GameObject> m_ListObj = new Dictionary<string,GameObject> ();
	public ObjData[] MyListObj;

	public void Awake(){
		for(int i = 0;i<l_ListObj.Count;i++){
			if(l_ListObj[i].obj!=null && l_ListObj[i].objname!=null && l_ListObj[i].objname!=""){
				if(m_ListObj.ContainsKey(l_ListObj[i].objname)){
					Debug.Log("重复Key:"+l_ListObj[i].objname);
				}
				else
					m_ListObj.Add(l_ListObj[i].objname,l_ListObj[i].obj);
			}
		}
	}
	public void AddObj(string name,GameObject target){
		ObjData objdata = new ObjData ();
		objdata.obj = target;
		objdata.objname = name;
		if (target == null)
			return;
		for (int i = 0; i < l_ListObj.Count; i++) {
			if (l_ListObj [i].obj == null) {
				l_ListObj.RemoveAt (i);
				i--;
			} else {
				if (l_ListObj [i].objname == name && l_ListObj[i].obj == target) {
					return;
				}
			}
		}
		l_ListObj.Add (objdata);
	}
	public GameObject GetObj(string objname){
		if (m_ListObj.ContainsKey (objname))
			return m_ListObj [objname];
		for(var i = 0;i < l_ListObj.Count;i++){
			if(l_ListObj[i].objname == objname)
				return l_ListObj[i].obj;
		}
		return null;
	}
	public Transform GetTransform(string objname){
		Transform tr;
		try{
			GameObject uigrid;
			uigrid = GetObj (objname);
			if(uigrid!=null){
				tr = uigrid.transform;
				return tr;
			}
			else
				return null;
		}
		catch{
			tr = null;
			Debug.Log("找不到名为:["+objname+"]的物体");
		}
		return tr;
	}
	public GameObject GetGameObject(string objname){
		GameObject obj;
		try{
			obj = GetObj (objname);
		}	
		catch{
			Debug.Log("找不到名为:["+objname+"]的物体");
			obj = null;
		}
		return obj;
	}
	public ObjData GetObj(int index){
		return l_ListObj[index];
	}
	public void SetColor(string name,Color color){
		try{
			UIWidget widget = GetObj(name).GetComponent<UIWidget>();
			widget.color = color;
		}
		catch{
			Debug.Log("找不到名为:["+name+"]的物体");
		}
	}
	public void SetActive(string name,bool boolean){
		try{
			GetObj(name).SetActive(boolean);
		}
		catch{
			Debug.Log("找不到名为:["+name+"]的物体");
		}
	}
	#region 各种调用
	public void SetSlider(string name,float value){
		try{
			UISlider _slider;
			_slider = GetGameObject (name).GetComponent<UISlider> ();
			_slider.value = value;
		}catch{
			Debug.Log ("UnFindLabel:" + name);
		}
	}
	public void SetLabel(string name,string label){
		try{
		UILabel m_uilab;
		m_uilab = GetGameObject (name).GetComponent<UILabel> ();
		m_uilab.text = label;
		}catch{
			Debug.Log ("UnFindLabel:" + name);
		}
	}
	public void SetSprite(string name,string sprite){
		try{
			UISprite m_uispr;
			m_uispr = GetGameObject (name).GetComponent<UISprite> ();
			m_uispr.spriteName = sprite;
		}catch{
			Debug.Log ("UnFindSprite:" + name);
		}
	}
	public void SetToggle(string name,bool value){
		try{
			UIToggle toggle;
			toggle = GetGameObject (name).GetComponent<UIToggle> ();
			toggle.value = value;
		}catch{
			Debug.Log ("UnFindSprite:" + name);
		}
	}
	public string GetInputValue(string key){
		try{
			UIInput input = GetGameObject(key).GetComponent<UIInput>();
			return input.value;
		}catch{
			return string.Format("cant find {0} input",key);
		}
	}
	public void ShowGameObjectAutoHide(string key,float time = 0){
	
		var obj = GetGameObject(key);
		if(obj == null)
			return;
		obj.SetActive(true);
		if(time >0){
			StartCoroutine(yieldDoSomething(time,delegate(){
				obj.SetActive(false);
			}));
		}
	}
	IEnumerator yieldDoSomething(float time,System.Action callback){
		yield return new WaitForSeconds(time);
		callback();
	}
	#endregion
}
[System.Serializable]
public class ObjData{
	public string objname;
	public GameObject obj;
	public void SetActive(bool boolean){
		obj.SetActive(boolean);
	}
	public GameObject GetGameObject(){
		return obj;
	}
	public Transform GetTransform(){
		return obj.transform;
	}
}
